<template>
  <div id="navbar">
    <div class="navbar">
      <div>
        <span class="left" @click="goToPath('/')">首页</span>
        <span class="left" @click="goToPath('/write')">创作</span>
      </div>
      <div v-if="!userInfo.user_id">
        <span class="right" @click="goToPath('/register')">注册</span>
        <span class="right" @click="goToPath('/login')">登录</span>
      </div>
      <div v-else>
        <span
          class="right"
          @click="showLogout(true)"
          @mouseenter="showLogout(true)"
          @mouseleave="showLogout(false)"
        >{{userInfo.user_nickname?userInfo.user_nickname:userInfo.user_email}}</span>
        <div
          class="right-box"
          @mouseenter="showLogout(true)"
          @mouseleave="showLogout(false)"
          v-if="showRightBoxStatus"
        >
          <span>我的博客</span>
          <span @click="goToPath('/login')">退出登录</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "navbar",
  data() {
    return {
      userInfo: {
        user_id: 0
      },
      showRightBoxStatus: false
    };
  },
  mounted() {
    const userInfo = JSON.parse(window.localStorage.getItem("blogUserInfo"));
    if (userInfo) {
      this.userInfo = userInfo;
    }
  },
  methods: {
    goToPath: function(path) {
      this.$router.push(path);
    },
    showLogout(status) {
      this.showRightBoxStatus = status;
    }
  }
};
</script>

<style scoped>
.navbar {
  width: 100%;
  margin: 0;
  padding: 0;
  background: #000;
  color: #fff;
  text-align: left;
  overflow: hidden;
  position: fixed;
  top: 0;
  left: 0;
}
.navbar span {
  width: 60px;
  height: 40px;
  line-height: 40px;
  display: block;
  cursor: pointer;
}
.left {
  float: left;
  font-size: 16px;
  margin-left: 25px;
}

.right-box {
  position: fixed;
  right: 0;
  top: 40px;
}
.right {
  float: right;
  font-size: 12px;
  color: #ccc;
}
.right-box span {
  z-index: 1000;
  background: #000;
  width: 80px;
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 12px;
  cursor: pointer;
}
</style>
